from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_stock.entity.do.basic_do import StockBasic
from module_stock.entity.vo.basic_vo import BasicModel, BasicPageQueryModel
from utils.db_util import DbUtil
from utils.page_util import PageUtil


class BasicDao:
    """
    股票基础信息模块数据库操作层
    """

    @classmethod
    async def get_basic_detail_by_id(cls, db: AsyncSession, symbol: int):
        """
        根据获取股票基础信息详细信息

        :param db: orm对象
        :param symbol: 
        :return: 股票基础信息信息对象
        """
        basic_info = (
            (
                await db.execute(
                    select(StockBasic)
                    .where(
                        StockBasic.symbol == symbol
                    )
                )
            )
            .scalars()
            .first()
        )

        return basic_info

    @classmethod
    async def get_basic_detail_by_info(cls, db: AsyncSession, basic: BasicModel):
        """
        根据股票基础信息参数获取股票基础信息信息

        :param db: orm对象
        :param basic: 股票基础信息参数对象
        :return: 股票基础信息信息对象
        """
        basic_info = (
            (
                await db.execute(
                    select(StockBasic).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return basic_info

    @classmethod
    async def get_all_symbols(cls, db: AsyncSession):
        """
        获取所有股票信息列表，并且不进行序列化和转化

        :param db: orm对象
        :return: 股票基础信息列表信息列表
        """
        query = (
            select(StockBasic.symbol)
            .order_by(StockBasic.symbol)
        )

        basic_list = await DbUtil.get_data(db, query)

        return basic_list

    @classmethod
    async def get_basic_list(cls, db: AsyncSession, query_object: BasicPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取股票基础信息列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 股票基础信息列表信息对象
        """
        query = (
            select(StockBasic)
            .where(
                StockBasic.name.like(f'%{query_object.name}%') if query_object.name else True,
            )
            .order_by(StockBasic.symbol)
            .distinct()
        )
        basic_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return basic_list

    @classmethod
    async def add_basic_dao(cls, db: AsyncSession, basic: BasicModel):
        """
        新增股票基础信息数据库操作

        :param db: orm对象
        :param basic: 股票基础信息对象
        :return:
        """
        db_basic = StockBasic(**basic.model_dump(exclude={}))
        db.add(db_basic)
        await db.flush()

        return db_basic
    
    @classmethod
    async def batch_add_basic_dao(cls, db: AsyncSession, basics: list[BasicModel]):
        """
        批量新增股票基础信息数据库操作

        :param db: orm对象
        :param basics: 股票基础信息对象列表
        :return:
        """
        db_basics = []
        for basic in basics:
            db_basic = StockBasic(**basic.model_dump(exclude={}))
            db_basics.append(db_basic)
        db.add_all(db_basics)
        await db.flush()
        return 0

    @classmethod
    async def edit_basic_dao(cls, db: AsyncSession, basic: dict):
        """
        编辑股票基础信息数据库操作

        :param db: orm对象
        :param basic: 需要更新的股票基础信息字典
        :return:
        """
        await db.execute(update(StockBasic), [basic])

    @classmethod
    async def delete_basic_dao(cls, db: AsyncSession, basic: BasicModel):
        """
        删除股票基础信息数据库操作

        :param db: orm对象
        :param basic: 股票基础信息对象
        :return:
        """
        await db.execute(delete(StockBasic).where(StockBasic.symbol.in_([basic.symbol])))

    @classmethod
    async def batch_delete_basic_dao(cls, db: AsyncSession, symbols):
        """
        批量删除股票基础信息数据库操作

        :param db: orm对象
        :param basic: 股票基础信息对象
        :return:
        """
        await db.execute(delete(StockBasic).where(StockBasic.symbol.in_(symbols)))

